package io.leo.leodata.utils;

import android.graphics.Bitmap;
import android.util.Log;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.OutputStream;
import java.io.Reader;
import java.util.List;

import static android.content.ContentValues.TAG;

public class FileUtils {
    public static void readFile(String path,ReadCallback callback){
        try {
            int lineNumber=0;
            LineNumberReader reader = readFile(path);
            String line;
            while ((line=reader.readLine())!=null){
                callback.onReadLine(++lineNumber,line);
            }
        } catch (Exception e) {
            callback.onError(e);
        }
    }

    public static LineNumberReader readFile(String path) throws FileNotFoundException {
        File file = new File(path);
        LineNumberReader reader = new LineNumberReader(new InputStreamReader(new FileInputStream(file)));
        return reader;
    }
    public static void mkdirsIfNotExists(List<String> dirs){
        for (String dir:dirs){
            mkdirIfNotExists(dir);
        }
    }

    public static void mkdirIfNotExists(String dir){
        File file = new File(dir);
        if (!file.exists()||file.isFile())
            file.mkdirs();
    }

    public static void writeTextFile(String filePath, String content) throws IOException {
        writeTextFile(filePath,content,false);
    }
    public static void writeTextFile(String path,String text,boolean append) throws IOException {
        File file = new File(path);
        createFileIfNotExists(file);
        FileWriter writer = new FileWriter(file,append);
        writer.write(text);
        writer.close();
    }

    public static void createFileIfNotExists(File file) throws IOException {
        if (!file.getParentFile().exists())
            mkdirIfNotExists(file.getParent());
        if (!file.exists())
            file.createNewFile();
    }

    public static void deleteFile(String path){
        File f = new File(path);
        f.deleteOnExit();
    }

    public static void moveFile(String src,String to) throws IOException {
        File f = new File(src);
        if (!f.exists())
            throw new FileNotFoundException("File "+src+" is not found");
        deleteFile(to);
        File t = new File(to);
        int byteread = 0;
        InputStream inStream = new FileInputStream(f);  //读入原文件
        FileOutputStream fs = new FileOutputStream(t);
        byte[] buffer = new byte[1444];
        while ((byteread = inStream.read(buffer)) != -1) {
            fs.write(buffer, 0, byteread);
        }
        inStream.close();
        fs.close();
        deleteFile(src);
    }

    public static void writeBitmapToPngFile(String path, Bitmap bitmap) throws IOException {
        File f = new File(path);
        f.deleteOnExit();

        FileOutputStream out = new FileOutputStream(f);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
        out.close();
    }
}
